//
//  RxDataSourcesCollectionViewTest2ViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources

class RxDataSourcesCollectionViewTest2ViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    let disposeBag = DisposeBag()
    

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "35.2.单分区的CollectionView - 使用自定义的Section"
        view.backgroundColor = .white
            
        test()
    }
    
    func test() {
        //定义布局方式以及单元格大小
        let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = CGSize(width: 100, height: 70)
        
        //创建一个重用的单元格
        collectionView.register(UINib(nibName: "TitleCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell")
        
        //初始化数据
        let items = Observable.just([
            MySection(header: "", items: [
                "Swift",
                "PHP",
                "Python",
                "Java",
                "javascript",
                "C#"
                ])
            ])
         
        //创建数据源
        let dataSource = RxCollectionViewSectionedReloadDataSource
            <MySection>(
            configureCell: { (dataSource, collectionView, indexPath, element) in
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! TitleCollectionViewCell
                cell.titleL.text = "\(indexPath.row)：\(element)"
                return cell}
        )
         
        //绑定单元格数据
        items
            .bind(to: collectionView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }

}
